package com.atguigu.queue;

import java.util.Scanner;

/**
 * 数组模拟环形队列的实现
 * @author WangJX
 * @version 1.0
 * @date 2019/9/15 23:42
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue2 queue = new ArrayQueue2(3);
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        char key = ' ';
        while (flag){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加队列");
            System.out.println("g(get): 获取队列");
            System.out.println("h(head): 获取头队列");
            key = sc.next().charAt(0);
            switch (key){
                case 's':{
                    try {
                        queue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                }
                case 'e':{
                    flag = false;
                    break;
                }
                case 'a':{
                    System.out.print("输入要添加的数：");
                    int value = sc.nextInt();
                    try {
                        queue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                }
                case 'g':{
                    try {
                        int result = queue.getQueue();
                        System.out.printf("出去的数据时：%d\n",result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                }
                case 'h':{
                    try {
                        int head = queue.headQueue();
                        System.out.printf("头队列是：%d\n", head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                }
                default:
                    break;
            }
        }

        System.out.println("程序退出");
    }
}
class ArrayQueue2{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;


    //初始化参数
    public ArrayQueue2(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        this.arr = new int[maxSize];
        this.front = 0; //队列的第一个元素
        this.rear = 0;  //队列的最后一个元素的后一个位置
    }

    //判断队列是否满
    public boolean isFull(){
        return (1 + rear) % maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return front == rear;
    }

    //添加数据
    public void addQueue(int n){
        if (isFull()){
            throw new RuntimeException("队列已满！");
        }
        //插入数据
        arr[rear] = n;
        //将rear后移，这里必须考虑取模
        rear = (rear + 1) % maxSize;
    }


    //获得数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //显示队列所有数据
    public void showQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //对列中有效的数据个数
    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列的头
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列是空的");
        }
        return arr[front];
    }
}